﻿using System;
using CodeCounsel.SharePoint.RibbonDesigner.Model;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Globalization;
using System.Windows.Interop;
using System.Windows;
using System.Drawing;
using System.IO;
using System.Xml.Linq;
using System.ComponentModel;
using System.Windows.Threading;
using System.Threading;
using System.Diagnostics;

namespace CodeCounsel.SharePoint.RibbonDesigner.ViewModel
{
    [ToolboxItem(true)]
    public class ButtonViewModel
        : ControlViewModel<Button>
    {
        public ImageSource LargeImage
        {
            get
            {
                ImageSource source = null;
                if (String.IsNullOrEmpty(Model.Image32by32) == false &&
                    Model.Image32by32.StartsWith(
                        "/_layouts", StringComparison.OrdinalIgnoreCase))
                {
                    string path = Model.Image32by32.Replace('/', '\\').ToLower();
                    path = path.Replace("_layouts", "layouts");
                    // TODO: REPLACE WITH CORRECT INSTALLED CULTURE
                    path = path.Replace("$resources:core,language;", "1033");
                    path = path.Replace("$resources: core,language;", "1033");
                    path = path.Replace("$resources: core, language;", "1033");
                    path = path.Replace("$resources:core, language;", "1033");
                    path = path.Replace("$resources : core,language;", "1033");
                    path = path.Replace("$resources : core, language;", "1033");
                    if (path.IndexOf('?') > -1)
                    {
                        path = path.Substring(0, path.IndexOf('?'));
                    }
                    string basePath = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE";
                    path = basePath + path;
                    if (File.Exists(path))
                    {
                        BitmapImage img = new BitmapImage(
                            new Uri(path));
                        source = new CroppedBitmap(img, new Int32Rect(
                            -1 * Model.Image32by32Left, -1 * Model.Image32by32Top, 32, 32));
                    }
                }
                if(source == null)
                {
                    source = Imaging.CreateBitmapSourceFromHBitmap(
                        Properties.Resources.Empty32.GetHbitmap(),
                        IntPtr.Zero, Int32Rect.Empty, 
                        BitmapSizeOptions.FromWidthAndHeight(32, 32));
                }
                return source;
            }
        }

        public ImageSource SmallImage
        {
            get
            {
                //_layouts/$Resources:core,Language;/images/formatmap16x16.png?vk=4536
                ImageSource source = null;
                if (String.IsNullOrEmpty(Model.Image16by16) == false &&
                    Model.Image16by16.StartsWith(
                        "/_layouts", StringComparison.OrdinalIgnoreCase))
                {
                    string path = Model.Image16by16.Replace('/', '\\').ToLower();
                    path = path.Replace("_layouts", "layouts");
                    // TODO: REPLACE WITH CORRECT INSTALLED CULTURE
                    path = path.Replace("$resources:core,language;", "1033");
                    path = path.Replace("$resources: core,language;", "1033");
                    path = path.Replace("$resources: core, language;", "1033");
                    path = path.Replace("$resources:core, language;", "1033");
                    path = path.Replace("$resources : core,language;", "1033");
                    path = path.Replace("$resources : core, language;", "1033");
                    if (path.IndexOf('?') > -1)
                    {
                        path = path.Substring(0, path.IndexOf('?'));
                    }
                    string basePath = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE";
                    path = basePath + path;
                    BitmapImage img = new BitmapImage(
                        new Uri(path));
                    CroppedBitmap bmp = new CroppedBitmap(img, new Int32Rect(
                        -1 * Model.Image16by16Left, -1 * Model.Image16by16Top, 16, 16));
                    return bmp;
                }
                else
                {
                    source = Imaging.CreateBitmapSourceFromHBitmap(
                        Properties.Resources.Empty16.GetHbitmap(),
                        IntPtr.Zero, Int32Rect.Empty,
                        BitmapSizeOptions.FromWidthAndHeight(16, 16));
                }
                return source;
            }
        }

        public string TemplateAlias
        {
            get { return Model.TemplateAlias; }
        }

        public string Title
        {
            get { return Model.LabelText; }
        }

        public ButtonViewModel()
            : base(new Button(), DisplayMode.Default)
        {
        }
        
        public ButtonViewModel(Button button, DisplayMode displayMode)
            : base(button, displayMode)
        {
        }
    }
}
